<script setup name="updateDialog" lang="ts">
import { ref, defineProps, defineEmits } from 'vue'
import { ElMessage } from 'element-plus'
import { updatePriceWeightApi } from '@/apis/order'

interface Props {
  visible: boolean,
  info: any
}

const props = defineProps<Props>()
const emit = defineEmits(['confirm', 'update:visible'])


watch(() => props.visible, (val) => {
  if (val && props.info) {
    formData.value = {
      shop_base_price: props.info.shop_base_price || '',
      shop_base_weight: props.info.shop_base_weight || '',
      shop_inventory: props.info.shop_inventory || 0,
      shop_car_price: props.info.shop_car_price || 0,
    }
  }
})

const formData = ref({
  shop_base_price: '',
  shop_base_weight: '',
  shop_inventory: 0,
  shop_car_price: 0,
})

/** 关闭弹窗 */
const handleClose = () => {
  formData.value = {
    shop_base_price: '',
    shop_base_weight: '',
    shop_inventory: 0,
    shop_car_price: 0,
  }
  emit('update:visible', false)
  console.log('关闭')
}

const handleConfirm = async () => {
  const res = await updatePriceWeightApi({...formData.value, id: props.info._id})
  ElMessage.success(res.msg)
  emit('confirm')
  handleClose()
}
</script>

<template>
  <el-dialog :model-value="visible" title="修改基础配置" width="400" @close="handleClose">
    <!-- 对话框内容插槽 -->
    <el-form>
      <el-form-item label="物流价格">
        <el-input v-model="formData.shop_base_price" placeholder="请输入物流价格" />
      </el-form-item>
      <el-form-item label="物流重量">
        <el-input v-model="formData.shop_base_weight" placeholder="请输入物流重量" />
      </el-form-item>
      <el-form-item label="库存数量">
        <el-input type="number" v-model="formData.shop_inventory" placeholder="请输入商品库存" />
      </el-form-item>
      <el-form-item label="采购价格">
        <el-input type="number" v-model="formData.shop_car_price" placeholder="请输入采购价格" />
      </el-form-item>
    </el-form>

    <!-- 自定义底部按钮 -->
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped></style>
